package com.tiyende.kash.loop.utils

import android.content.Context
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import androidx.core.content.ContextCompat

/**
 * SpannableString 工具类
 * 用于处理文本样式
 */
object TiyendeSpannableStringUtils {

    /**
     * 改变字符串中所有指定特殊字符之间的文本颜色并移除特殊字符
     *
     * @param text 源文本
     * @param startDelimiter 开始分隔符
     * @param endDelimiter 结束分隔符
     * @param color 要设置的颜色值
     * @param isBold 是否加粗 ，默认不加粗
     * @return 处理后的SpannableString
     */
    fun changeColorAndRemoveDelimiters(text: String, startDelimiter: String, endDelimiter: String, color: Int,isBold: Boolean = false): SpannableString {
        if (text.isEmpty() || startDelimiter.isEmpty() || endDelimiter.isEmpty()) {
            return SpannableString(text)
        }

        // 先移除所有特殊字符，得到纯文本
        val cleanText = text.replace("$startDelimiter$endDelimiter", "")
            .replace(startDelimiter, "")
            .replace(endDelimiter, "")

        val spannableString = SpannableString(cleanText)
        var currentIndex = 0
        var cleanTextIndex = 0

        while (currentIndex < text.length) {
            // 查找开始分隔符
            val startIndex = text.indexOf(startDelimiter, currentIndex)
            if (startIndex == -1) break

            // 查找结束分隔符
            val contentStart = startIndex + startDelimiter.length
            val endIndex = text.indexOf(endDelimiter, contentStart)
            if (endIndex == -1) break

            // 提取特殊字符之间的内容
            val content = text.substring(contentStart, endIndex)

            // 在清理后的文本中找到对应的位置
            val spanStart = cleanText.indexOf(content, cleanTextIndex)
            if (spanStart == -1) break

            val spanEnd = spanStart + content.length

            // 应用颜色
            val colorSpan = ForegroundColorSpan(color)
            spannableString.setSpan(colorSpan, spanStart, spanEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
            if (isBold){
                spannableString.setSpan(StyleSpan(Typeface.BOLD), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            }

            // 更新索引
            cleanTextIndex = spanEnd
            currentIndex = endIndex + endDelimiter.length
        }

        return spannableString
    }

    /**
     * 使用颜色资源版本
     */
    fun changeColorAndRemoveDelimitersWithResource(context: Context, text: String, startDelimiter: String, endDelimiter: String, colorRes: Int): SpannableString {
        val color = ContextCompat.getColor(context, colorRes)
        return changeColorAndRemoveDelimiters(text, startDelimiter, endDelimiter, color)
    }

    /**
     * 支持多个分隔符对的版本
     */
    fun changeColorsAndRemoveDelimiters(text: String, vararg delimiterColorPairs: Pair<Pair<String, String>, Int>): SpannableString {
        if (text.isEmpty() || delimiterColorPairs.isEmpty()) {
            return SpannableString(text)
        }

        // 先移除所有特殊字符
        var cleanText = text
        delimiterColorPairs.forEach { (delimiters, _) ->
            val (start, end) = delimiters
            cleanText = cleanText.replace("$start$end", "")
                .replace(start, "")
                .replace(end, "")
        }

        val spannableString = SpannableString(cleanText)

        delimiterColorPairs.forEach { (delimiters, color) ->
            val (startDelimiter, endDelimiter) = delimiters
            var currentIndex = 0
            var cleanTextIndex = 0

            while (currentIndex < text.length) {
                val startIndex = text.indexOf(startDelimiter, currentIndex)
                if (startIndex == -1) break

                val contentStart = startIndex + startDelimiter.length
                val endIndex = text.indexOf(endDelimiter, contentStart)
                if (endIndex == -1) break

                val content = text.substring(contentStart, endIndex)
                val spanStart = cleanText.indexOf(content, cleanTextIndex)
                if (spanStart == -1) break

                val spanEnd = spanStart + content.length

                val colorSpan = ForegroundColorSpan(color)
                spannableString.setSpan(colorSpan, spanStart, spanEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)

                cleanTextIndex = spanEnd
                currentIndex = endIndex + endDelimiter.length
            }
        }

        return spannableString
    }
}